<?php

namespace app\admin\controller;

use think\Db;
use \app\admin\model\Product as ProductModel;

class Index extends Main
{
    /**
     * 首页展示
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 桌面页
     */
    public function welcome()
    {
        //当天凌晨时间戳
        $todatTime = strtotime(date('Y-m-d'));
        //昨日凌晨时间戳
        $yesterdayTime = strtotime(date("Y-m-d", strtotime("-1 day")));

        $company_id = isession('company_id');
        //亚马逊订单
        //订单总数
        $totalOrder = DB::name('amazon_order')->where("company_id",$company_id)->count();
        //今日订单数
        $todayOrder = DB::name('amazon_order')->where("company_id=".$company_id." AND create_time >" . $todatTime)->count();
        //昨日订单数
        $yesterdayOrder = DB::name('amazon_order')->where("company_id=".$company_id." AND create_time > " . $yesterdayTime . " AND create_time<" . $todatTime)->count();

        $ProductModel = new ProductModel();
        //商品总数
        $totalGood = $ProductModel->countGood($company_id);
        //今日商品数
        $todayGood = $ProductModel->countGood($company_id, "timestamp >" . $todatTime);
        //昨日商品数
        $yesterdayGood = $ProductModel->countGood($company_id, "timestamp > " . $yesterdayTime . " AND timestamp<" . $todatTime);

        //运单总数
        $totalWaybill = DB::name('waybill')->where("company_id",$company_id)->count();
        //今日运单数
        $todayWaybill = DB::name('waybill')->where("company_id=".$company_id." AND timestamp >" . $todatTime)->count();
        //昨日运单数
        $yesterdayWaybill = DB::name('waybill')->where("company_id=".$company_id." AND timestamp > " . $yesterdayTime . " AND timestamp<" . $todatTime)->count();

        //采购总数
        $totalPurchase = DB::name('purchase')->where("company_id",$company_id)->count();
        //今日运单数
        $todayPurchase = DB::name('purchase')->where("company_id=".$company_id." AND timestamp >" . $todatTime)->count();
        //昨日运单数
        $yesterdayPurchase = DB::name('purchase')->where("company_id=".$company_id." AND timestamp > " . $yesterdayTime . " AND timestamp<" . $todatTime)->count();

        //通知公告
        $notice = Db::name('notice')->where('type',1)->order('odx asc,timestamp desc')->paginate(15);

        $this->assign('notice', $notice);
        $this->assign('totalOrder', $totalOrder);
        $this->assign('todayOrder', $todayOrder);
        $this->assign('yesterdayOrder', $yesterdayOrder);
        $this->assign('totalGood', $totalGood);
        $this->assign('todayGood', $todayGood);
        $this->assign('yesterdayGood', $yesterdayGood);
        $this->assign('totalWaybill', $totalWaybill);
        $this->assign('todayWaybill', $todayWaybill);
        $this->assign('yesterdayWaybill', $yesterdayWaybill);
        $this->assign('totalPurchase', $totalPurchase);
        $this->assign('todayPurchase', $todayPurchase);
        $this->assign('yesterdayPurchase', $yesterdayPurchase);
        return $this->fetch();
    }

}
